from Baseline_Greedy import Greedy_Decision
from ExperimentSimulation import EventLine
from GeneticAlgorithm import GA_Decision
from Baseline_Random import Random_Decision
from BasicDefinition import Network
from matplotlib import pyplot as plt
from Baseline_DQN import DQN_Decision
from Baseline_DQN import GetAgent

seqlen = 15
ave = 3

GA_Drop_Seq, Greedy_Drop_Seq, Random_Drop_Seq, DQN_Drop_Seq = ([0 for _ in range(1, seqlen)],
                                                               [0 for _ in range(1, seqlen)],
                                                               [0 for _ in range(1, seqlen)],
                                                               [0 for _ in range(1, seqlen)])
GA_Time_Seq, Greedy_Time_Seq, Random_Time_Seq, DQN_Time_Seq = ([0 for _ in range(1, seqlen)],
                                                               [0 for _ in range(1, seqlen)],
                                                               [0 for _ in range(1, seqlen)],
                                                               [0 for _ in range(1, seqlen)])
GA_Usage_Seq, Greedy_Usage_Seq, Random_Usage_Seq, DQN_Usage_Seq = ([0 for _ in range(1, seqlen)],
                                                                   [0 for _ in range(1, seqlen)],
                                                                   [0 for _ in range(1, seqlen)],
                                                                   [0 for _ in range(1, seqlen)])

X = [i for i in range(1, seqlen)]

for j in range(ave):
    TotalMission = 0
    for i in range(1, seqlen):
        network = Network(6, 6,
                          8, 3,
                          16, 2 * i,
                          2, 50,
                          25, 400,
                          3000, 2)
        Agent_List = [[GetAgent(network, j, i, 0.01, 0.95, 1, 0.01, 0.5, 10, 20, 3, 10)
                       for i in range(network.SatelliteNumber)] for j in range(network.OrbitNumber)]
        # for agent in Agent_List:
        #     print(agent.InputLength)
        Random = EventLine(network, Random_Decision, 2000, 3)
        DQN = EventLine(network, DQN_Decision, 2000, 3, Agent_List)
        GA = EventLine(network, GA_Decision, 2000, 10, 3, 20, pow(10, 20), 1, 20, 100, 20, 20)
        Greedy = EventLine(network, Greedy_Decision, 2000, 3)

        GA.Simulate()
        Greedy.Simulate()
        Random.Simulate()
        DQN.Simulate()

        GA_Time, GA_Drop, GA_Usage = GA.TotalTime, GA.TotalDrop, 1 - GA.MaximumUsage
        Greedy_Time, Greedy_Drop, Greedy_Usage = Greedy.TotalTime, Greedy.TotalDrop, 1 - Greedy.MaximumUsage
        Random_Time, Random_Drop, Random_Usage = Random.TotalTime, Random.TotalDrop, 1 - Random.MaximumUsage
        DQN_Time, DQN_Drop, DQN_Usage = DQN.TotalTime, DQN.TotalDrop, 1 - DQN.MaximumUsage
        TotalMission = GA.TotalMission

        GA_Time_Seq[i - 1] += GA_Time
        GA_Drop_Seq[i - 1] += GA_Drop / TotalMission
        GA_Usage_Seq[i - 1] += GA_Usage
        Greedy_Time_Seq[i - 1] += Greedy_Time
        Greedy_Drop_Seq[i - 1] += Greedy_Drop / TotalMission
        Greedy_Usage_Seq[i - 1] += Greedy_Usage
        Random_Time_Seq[i - 1] += Random_Time
        Random_Drop_Seq[i - 1] += Random_Drop / TotalMission
        Random_Usage_Seq[i - 1] += Random_Usage
        DQN_Time_Seq[i - 1] += DQN_Time
        DQN_Drop_Seq[i - 1] += DQN.TotalDrop / TotalMission
        DQN_Usage_Seq[i - 1] += DQN_Usage
        print("Finished:", j, i)

GA_Time_Seq = [ele / ave for ele in GA_Time_Seq]
GA_Drop_Seq = [ele / ave for ele in GA_Drop_Seq]
GA_Usage_Seq = [ele / ave for ele in GA_Usage_Seq]
Greedy_Time_Seq = [ele / ave for ele in Greedy_Time_Seq]
Greedy_Drop_Seq = [ele / ave for ele in Greedy_Drop_Seq]
Greedy_Usage_Seq = [ele / ave for ele in Greedy_Usage_Seq]
Random_Time_Seq = [ele / ave for ele in Random_Time_Seq]
Random_Drop_Seq = [ele / ave for ele in Random_Drop_Seq]
Random_Usage_Seq = [ele / ave for ele in Random_Usage_Seq]
DQN_Time_Seq = [ele / ave for ele in DQN_Time_Seq]
DQN_Drop_Seq = [ele / ave for ele in DQN_Drop_Seq]
DQN_Usage_Seq = [ele / ave for ele in DQN_Usage_Seq]

plt.plot(X, GA_Drop_Seq, 'b*--')
plt.plot(X, Greedy_Drop_Seq, 'r*--')
plt.plot(X, Random_Drop_Seq, 'g*--')
plt.plot(X, DQN_Drop_Seq, 'y*--')
plt.xlabel('Mission Incidence')
plt.ylabel('Drop Incidence')
plt.legend(['GA', 'Greedy', 'Random', 'DQN'], loc='upper left')
plt.show()

plt.plot(X, GA_Time_Seq, 'b*--')
plt.plot(X, Greedy_Time_Seq, 'r*--')
plt.plot(X, Random_Time_Seq, 'g*--')
plt.plot(X, DQN_Time_Seq, 'y*--')
plt.xlabel('Mission Incidence')
plt.ylabel('Processed Time')
plt.legend(['GA', 'Greedy', 'Random', 'DQN'], loc='upper left')
plt.show()

plt.plot(X, GA_Usage_Seq, 'b*--')
plt.plot(X, Greedy_Usage_Seq, 'r*--')
plt.plot(X, Random_Usage_Seq, 'g*--')
plt.plot(X, DQN_Usage_Seq, 'y*--')
plt.xlabel('Mission Incidence')
plt.ylabel('Maximum Usage')
plt.legend(['GA', 'Greedy', 'Random', 'DQN'], loc='upper left')
plt.show()
